package com.school.campus.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import cn.hutool.core.date.DateTime;
import com.school.campus.domain.Comment;
import com.school.campus.domain.vo.PostVO;
import com.school.campus.mapper.CommentMapper;
import com.school.campus.service.IpProvinceService;
import com.school.common.core.domain.entity.SysUser;
import com.school.common.utils.IpUtils;
import com.school.common.utils.SecurityUtils;
import com.school.system.mapper.SysUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.school.campus.mapper.PostMapper;
import com.school.campus.domain.Post;
import com.school.campus.service.IPostService;

import javax.servlet.http.HttpServletRequest;

/**
 * 帖子Service业务层处理
 *
 * @author wwj
 * @date 2025-11-03
 */
@Service
public class PostServiceImpl implements IPostService {
    @Autowired
    private PostMapper postMapper;

    @Autowired
    private SysUserMapper sysUserMapper;



    @Autowired
    private CommentMapper commentMapper;


    /**
     * 查询帖子
     *
     * @param id 帖子主键
     * @return 帖子
     */
    @Override
    public Post selectPostById(Long id) {
        return postMapper.selectPostById(id);
    }

    /**
     * 查询帖子列表
     *
     * @param post 帖子
     * @return 帖子
     */
    @Override
    public List<Post> selectPostList(Post post) {
        return postMapper.selectPostList(post);
    }

    /**
     * 新增帖子
     *
     * @param post 帖子
     * @return 结果
     */
    @Override
    public int insertPost(Post post) {
        Long userId = SecurityUtils.getUserId();
        SysUser sysUser = sysUserMapper.selectUserById(userId);
        post.setPublisher(SecurityUtils.getUsername());
        post.setAvatar(sysUser.getAvatar());
        post.setPublishTime(DateTime.now());

        post.setCommentCount(0L);
        post.setLikeCount(0L);
        return postMapper.insertPost(post);
    }

    /**
     * 修改帖子
     *
     * @param post 帖子
     * @return 结果
     */
    @Override
    public int updatePost(Post post) {
        return postMapper.updatePost(post);
    }

    /**
     * 批量删除帖子
     *
     * @param ids 需要删除的帖子主键
     * @return 结果
     */
    @Override
    public int deletePostByIds(Long[] ids) {
        return postMapper.deletePostByIds(ids);
    }

    /**
     * 删除帖子信息
     *
     * @param id 帖子主键
     * @return 结果
     */
    @Override
    public int deletePostById(Long id) {
        return postMapper.deletePostById(id);
    }

    @Override
    public int likePost(Long id) {
        Post post = postMapper.selectPostById(id);
        post.setLikeCount(post.getLikeCount() + 1);
        return postMapper.updatePost(post);
    }

    @Override
    public PostVO selectPostWithComments(Long id) {
        // 查询帖子基本信息
        Post post = postMapper.selectPostById(id);
        Comment comment = new Comment();
        comment.setPostId(id);
        // 查询帖子关联的评论列表
        List<Comment> commentList = commentMapper.selectCommentList(comment);

        // 构造 PostVO 对象
        PostVO postVO = new PostVO();
        // 复制 post 属性到 postVO
        BeanUtils.copyProperties(post, postVO);
        postVO.setCommentList(commentList);

        return postVO;
    }

}
